/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.*;
import com.saville.debug.*;
import com.saville.msgcomp.*;
import com.saville.mc.*;


public class TestHttpServer1 extends ActiveMc {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestHttpServer1();
    }

    TestHttpServer1() {
        super("TestHttpServer1");
        df = Log.VERBOSE;
        // mMcMgr.df = Log.VERBOSE;
        TcpMsgPipeConnectionServer tmpcs = new TcpMsgPipeConnectionServer(12345, this);

        if (!tmpcs.start()) {
            throw new RuntimeException("Could not start connection server");
        }
    }

    @Override
    public void processMsg(Msg pipeMsg) {
        Log.d(df, "%s: processMsg() E pipeMsg guid=0x%x cmd=0x%x", mName, pipeMsg.guid, pipeMsg.cmd);

        if (pipeMsg.is(McConst.MC_GUID, McConst.MC_CMD_STARTED)) {
            Log.v(df, "%s: MC_CMD_STARTED", mName);
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_CONNECTED)) {
            /*
             * Create a new pipe for this socket
             */
            mSocket = (Socket) pipeMsg.obj;

            mHmp = new HttpMsgPipe(mSocket, this.mMcId.id, 1);

        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_CLOSED)) {
            Log.d(df, "%s: processMsg() remove dstSubId=0x%x", mName, pipeMsg.dstSubId);
            //mSocket.close();
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_PRODUCER_MSG)) {
            Log.d(df, "%s: processMsg() remove dstSubId=0x%x", mName, pipeMsg.dstSubId);
//            Msg msg = (Msg) pipeMsg.obj;
//            TcpMsgPipe pipe = connMapping.get(pipeMsg.dstSubId);
//
//
//            if (msg.is(McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_REGISTER_DEV)) {
//                Log.d(df, "McMaster: processMsg() RegisterDev");
//                msg.obj = registerDevId((DevId) msg.obj);
//                Msg replyMsg = mMcMgr.getReplyMsg(msg, McConst.STATUS_OK);
//                pipe.queueMsg(replyMsg);
//            } else if (msg.is(McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_LOOKUP_DEV)) {
//                Log.d(df, "McMaster: processMsg() LookupDev");
//                Msg replyMsg;
//                
//                DevId devId = (DevId) msg.obj;
//                DevId lookupResult = lookupDevId(devId.urn);
//                if (lookupResult != null) {
//                    replyMsg = mMcMgr.getReplyMsg(msg, McConst.STATUS_OK);
//                    replyMsg.obj = lookupResult;
//                } else {
//                    replyMsg = mMcMgr.getReplyMsg(msg, McConst.STATUS_ERR);
//                }
//                pipe.queueMsg(replyMsg);
//            } else if (msg.is(McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_FORWARD_MSG)) {
//                Log.d(df, "McMaster: processMsg() ForwardMsg");
//                
//            } else if (msg.is(McConst.MC_GUID, McConst.MC_CMD_ECHO)) {
//                Log.d(df, "McMaster: processMsg() Echo");
//                pipe.queueMsg(getReplyMsg(msg, McConst.STATUS_ECHO));
//            } else {
//                Log.e(df, "McMaster: received unknown msg guid=0x%x cmd=0x%x", msg.guid, msg.cmd);
//                if (msg.cmdIsRequest()) {
//                    pipe.queueMsg(getReplyMsg(msg, McConst.STATUS_UNKNOWN_CMD));
//                }
//            }
        } else {
            Log.e(df, "%s: received unknown pipeMsg guid=0x%x cmd=0x%x", mName, pipeMsg.guid,
                pipeMsg.cmd);
            if (pipeMsg.cmdIsRequest()) {
                sendReplyMsg(pipeMsg, McConst.STATUS_UNKNOWN_CMD);
            }
        }

        Log.d(df, "%s: processMsg() X", mName);
    }
    
    HttpMsgPipe mHmp;
    Socket mSocket;
}
